package com.hssy.stationtv;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

import android.os.Bundle;
import android.os.Looper;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;


public class ButtonFour extends Activity{
	
	private Context context;
	
	private WebView localWebView;
	protected void onCreate(Bundle paramBundle){
	    super.onCreate(paramBundle);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	            WindowManager.LayoutParams.FLAG_FULLSCREEN);
	    setContentView(R.layout.realrecord);
	    init();
	}
	
	@SuppressLint("SetJavaScriptEnabled")
	private void init(){
	    localWebView = (WebView)findViewById(R.id.realrecord);
	    WebSettings settings = localWebView.getSettings();
	    settings.setJavaScriptEnabled(true);
	    localWebView.addJavascriptInterface(new JsInteration(), "control");
	    localWebView.setWebChromeClient(new WebChromeClient()
	    {
	    });
	    localWebView.setWebViewClient(new WebViewClient()
	    {
	      public void onPageFinished(WebView paramAnonymousWebView, String paramAnonymousString)
	      {
	        super.onPageFinished(paramAnonymousWebView, paramAnonymousString);
	      }
	    });
	    localWebView.loadUrl("file:///android_asset/html/realRecord.html");
	}

	public class JsInteration {
		public JsInteration() {
		}
		
		@JavascriptInterface
	    public String readFiles() {
			String content = null;
			try {
				FileInputStream fis = openFileInput(MainActivity.FILENAME);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) != -1) {
					baos.write(buffer, 0, len);
				}
				content = baos.toString();
				fis.close();
				baos.close();
			} catch (FileNotFoundException e) {
	            e.printStackTrace();
	            writeFiles("192.168.0.10");
	        } catch (Exception e) {
				e.printStackTrace();
			}
			return content; 
		}

		@JavascriptInterface
		public String getVerName() {
	        String verName = "";
	        try {
	            verName = context.getPackageManager().getPackageInfo(
	                    "com.hssy.stationtv", 0).versionName;
	        } catch (NameNotFoundException e) {
	        	e.printStackTrace();
	        }
	        return verName;
	    }
		
		@JavascriptInterface
		public void toastMessage(String paramString) {
			Toast.makeText(ButtonFour.this.getApplicationContext(), paramString, Toast.LENGTH_LONG).show();
		}
		
		@JavascriptInterface
		public void upgrade() {
		    new Thread(new Runnable(){
	            @Override
	            public void run() {
	    			Looper.prepare();
	            	UpdateManager manager = new UpdateManager(ButtonFour.this);
	    			manager.checkUpdate("realRecord");
	    			Looper.loop();
	            }
	        }).start();
		}
	}
	
	private void writeFiles(String content) {
		try {
			FileOutputStream fos = openFileOutput(MainActivity.FILENAME,
			Context.MODE_PRIVATE);
			fos.write(content.getBytes());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}